package com.exwave.socialgame.model_user.controller;

import com.exwave.socialgame.jwt_token.Token;
import com.exwave.socialgame.model_user.entity.User;
import com.exwave.socialgame.model_user.service.SignService;
import com.exwave.socialgame.jwt_token.TokenService;
import com.exwave.socialgame.model_user.service.UserService;
import com.exwave.socialgame.myutils.RedisForUser;
import com.exwave.socialgame.myutils.ReturnUtils;
import com.exwave.socialgame.myutils.ParamsException;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-10-19 16:12:46
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private SignService signService;
    @Resource
    private TokenService tokenService;
    @Resource
    private HttpServletRequest httpServletRequest;
    @Resource
    private RedisForUser redisForUser;

    /**
     * 通过电话查询单条数据
     *
     * @param utel 主键
     * @return 单条数据
     */
    @GetMapping("select")
    @Token.UserLoginToken
    @ApiOperation(value = "获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "utel", value = "手机号", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header")
    })
    public Object selectByTel(String utel) {
        User user;
        try {
            user = this.userService.queryByTel(utel);
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        HashMap<String, Object> map = new HashMap<>();
        user.setUpassword("******"); //隐藏密码
        map.put("user", user);
        return ReturnUtils.returnMap(200, "success", map);
    }

    /**
     * 昵称+电话+密码注册
     *
     * @param user 实例对象
     * @return 单条数据
     */
    @PostMapping("register")
    @ApiOperation(value = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uname", value = "昵称", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "utel", value = "电话号码", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "upassword", value = "密码", required = true, dataTypeClass = String.class, paramType = "query")
    })
    //获取到的数据自动封装到user里面，前提是变量名一致；
    public Object register(@ApiIgnore User user) {
        try {
            this.userService.insert(user);
            this.signService.login(user.getUtel(), user.getUpassword());
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        //构造返回信息
        Map<String, Object> map = new HashMap<>();
        map.put("user", this.userService.queryByTel(user.getUtel()));
        map.put("token", redisForUser.getLogin(user.getUid())); //获取Token返回
        return ReturnUtils.returnMap(200, "success", map);
    }

    /**
     * 更新用户信息
     *
     * @param user 实例对象
     * @return 单条数据
     */
    @PostMapping("update")
    @ApiOperation(value = "更新用户信息")
    @Token.UserLoginToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uname", value = "昵称", required = false, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "uage", value = "年龄", required = false, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "usex", value = "性别", required = false, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "upersonality", value = "个性签名", required = false, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "uqq", value = "QQ", required = false, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "uwechat", value = "微信", required = false, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header")
    })
    public Object update(@ApiIgnore User user) {
        String token = httpServletRequest.getHeader("token");
        try {
            user.setUid(this.tokenService.getUidByToken(token));
            this.userService.update(user);
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        return ReturnUtils.returnMap(200, "success");
    }

    /**
     * 注销用户
     *
     * @param user 对象
     * @return 处理结果
     */
    @PostMapping("delete")
    @ApiOperation(value = "注销用户")
    @Token.UserLoginToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户唯一识别码", required = true, dataTypeClass = int.class, paramType = "query"),
            @ApiImplicitParam(name = "utel", value = "手机号", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "upassword", value = "密码", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header")
    })
    public Object deleteUser(@ApiIgnore User user) {
        try {
            this.userService.deleteByUser(user);
            this.redisForUser.delLogin(user.getUid());
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        return ReturnUtils.returnMap(200, "success", "用户：" + user.getUname() + "已注销");
    }
}